﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineMovieTicket.Data.IRepository;
using OnlineMovieTicket.Model;

namespace OnlineMovieTicket.Data.Repository
{
    public class MovieReviewRepository : IMovieReviewRepository
    {
        ONLINEMOVIETICKETEntities dataContent;

        public MovieReviewRepository()
        {
            dataContent = new ONLINEMOVIETICKETEntities();
        }

        public IList<MovieReview> GetReviews()
        {
            var movieReviewList = (from dt in dataContent.Reviews
                                   select new MovieReview
                                   {
                                       ReviewID = dt.ReviewID,
                                       MovieCode = dt.MovieCode,
                                       DetailReview = dt.DetailReview,
                                       CustomerName = dt.CustomerName,
                                       Email = dt.Email,
                                       ReviewDate = DateTime.Now,
                                       Rating = (Int32)dt.Rating,

                                   }).ToList();
            return movieReviewList;
        }

        public IList<MovieReview> GetReviews(int movieCode)
        {            
            var rmovie = (from dt in dataContent.Reviews
                         where dt.MovieCode == movieCode
                         select new MovieReview
                            {
                                ReviewID = dt.ReviewID,
                                MovieCode = dt.MovieCode,
                                DetailReview = dt.DetailReview,
                                CustomerName = dt.CustomerName,
                                Email = dt.Email,
                                ReviewDate = DateTime.Now,
                                Rating = (Int32)dt.Rating,

                            }).ToList();

            return rmovie;
        }

        public void Save(MovieReview movieReview, int mode)
        {
            if (mode == 0)
            {
                dataContent.AddToReviews(new Review
                {
                    ReviewID = GetNewReviewID(),
                    MovieCode = movieReview.MovieCode,
                    DetailReview = movieReview.DetailReview,
                    CustomerName = movieReview.CustomerName,
                    Email = movieReview.Email,
                    ReviewDate = movieReview.ReviewDate,
                    Rating = movieReview.Rating

                });
            }
            else
            {
                var dbReview = dataContent.Reviews.FirstOrDefault(m => m.MovieCode == movieReview.MovieCode);

                dbReview.CustomerName = movieReview.CustomerName;
                dbReview.Email = movieReview.Email;
                dbReview.DetailReview = movieReview.DetailReview;
                dbReview.ReviewDate = movieReview.ReviewDate;
                dbReview.Rating = movieReview.Rating;
            }
            dataContent.SaveChanges();
        }

        public bool Delete(int movieCode)
        {
            bool returnVal = false;
            try
            {
                var data = dataContent.Reviews.FirstOrDefault(m => m.MovieCode == movieCode);
                if (data != null)
                {
                    dataContent.Reviews.DeleteObject(data);
                    dataContent.SaveChanges();
                    returnVal = true;
                }
                else
                    throw new Exception("Record not found");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returnVal;
        }

        public int GetNewReviewID()
        {
            int newCode = 0;

            IList<Model.MovieReview> movieReviewList = GetReviews();

            if (movieReviewList != null && movieReviewList.Count > 0)
                newCode = movieReviewList[movieReviewList.Count - 1].ReviewID;

            return newCode + 1;

        }
    }
}
